#pragma once

#if C2504
#pragma message("CelSphere.h");
#endif

#include "D3D11Client.h"
#include "D3D11Effect.h"
#include "TileManager.h"

#define MAXSTARVERTEX 32768
#define NSEG 64


class D3D11Effect;
class Scene;

struct StarRec {
	float lng, lat, mag;
};

struct STARVERTEX {
	D3DXVECTOR3 pos;
	float col;			//16
};

class CelBackground : public D3D11Effect {
public:
	CelBackground(D3D11Client *pGC);
	~CelBackground();

	void SaveParams(int level, int bglvl);
	void Render( /*int level, int bglvl*/ );
private:
	void ProcessTile( int hemisphere, int lvl, int ilat, int nlat, int ilng, int nlng, TILEDESC *tile,
		const TEXCRDRANGE &range, ID3D11ShaderResourceView *tex );

	void RenderTile( int hemisphere, int lvl, int ilat, int nlat, int ilng, int nlng,
		TILEDESC *tile, const TEXCRDRANGE &range, ID3D11ShaderResourceView *tex );

	VECTOR3 GetTileCenter( int hemisphere, int ilat, int nlat, int ilng, int nlng );
	void SetWorldMatrix( int ilat, int nlat, int ilng, int nlng );
	bool IsTileInView( int lvl, int ilat );

	void LoadTextures();

	TILEMESH **TPL;
	int **NLNG, *NLAT, *patchidx;

	MATRIX3 ecl2gal;
	D3DXMATRIX trans, mWorld;
	struct RPARAM {
		D3DXMATRIX mWorld;
		int tgtlvl;
		VECTOR3 CamDir;
		double viewap;
	} RP;

	TILEDESC *tiledesc;
	ID3D11ShaderResourceView **texbuf;
	DWORD ntex;
	bool disabled;
	float Intensity;
	char texname[64];

	TileBuffer *tbuf;
	int maxlvl, maxbaselvl;
	const D3D11Config *cfg;

	//MT
	float intens;

	// Graphics Client
	D3D11Client *mGraphicsClient;

	// Scene
	Scene *mScene;

	// Config
	D3D11Config *mConfig;
};
